package targetToOffer.tenthDay;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Godc
 * @description
 */
public class PathSum {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(1);
        TreeNode treeNode1 = new TreeNode(2);
        TreeNode treeNode2 = new TreeNode(3);
        treeNode.left = treeNode1;
        treeNode.right = treeNode2;
        PathSumSolution s = new PathSumSolution();
        s.pathSum(treeNode,4);

    }
}

class PathSumSolution {
    LinkedList<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int target) {
        dfs(root,target);
        return res;
    }
    public void dfs(TreeNode root,int target){
        if(root == null) return;
        target-=root.val;
        path.add(root.val);
        if(target==0&&root.left==null&&root.right==null){
            res.add(new LinkedList<>(path));
        }
        dfs(root.left,target);
        dfs(root.right,target);
        path.removeLast();
    }
}
class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}